package com.freedom.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.freedom.bean.core.SysMenu;
import com.freedom.bean.core.SysRole;
import com.freedom.bean.core.SysRoleMenu;
import com.freedom.bean.core.SysUser;
import com.freedom.bean.core.SysUserRole;
import com.freedom.dao.core.SysMenuDao;
import com.freedom.dao.core.SysRoleDao;
import com.freedom.dao.core.SysRoleMenuDao;
import com.freedom.dao.core.SysUserDao;
import com.freedom.dao.core.SysUserRoleDao;
//import com.freedom.util.sys.RedisUtil;
import com.freedom.util.sys.SpringContextHolder;

/**
 * 系统的用户工具类
 * @author HGJ
 *
 */
public class UserUtils {
	
	private static SysUserDao sysUserDao = (SysUserDao)SpringContextHolder.getBean(SysUserDao.class);
	private static SysUserRoleDao sysUserRoleDao = (SysUserRoleDao)SpringContextHolder.getBean(SysUserRoleDao.class);
	private static SysRoleMenuDao sysRoleMenuDao = (SysRoleMenuDao)SpringContextHolder.getBean(SysRoleMenuDao.class);
	private static SysMenuDao sysMenuDao = (SysMenuDao)SpringContextHolder.getBean(SysMenuDao.class);
	private static SysRoleDao sysRoleDao = (SysRoleDao)SpringContextHolder.getBean(SysRoleDao.class);
	//所有缓存都必须缓存当前用户的，以用户的id参数为标识唯一
	public static final String USER_CACHE = "userCache"; //当前用户
	public static final String USER_ROLE_CACHE = "userRoleCache"; //用户角色的权限集合
	public static final String ROLE_MENU_CACHE = "userRoleMenuCache"; //角色菜单的权限集合
	public static final String SYS_MENU = "sysMenuCache"; //用户菜单的列表
	public static final String SYS_ROLE = "sysRoleCache"; //用户角色列表
	
	/**
	 * 根据ID获取用户
	 * @param id
	 * @return 取不到返回null
	 */
	public static SysUser get(String id){
		/*SysUser user = (SysUser)RedisUtil.get(USER_CACHE+id);
		if (user ==  null){
			user = sysUserDao.get(id);
			RedisUtil.set(USER_CACHE+id, user);
			if (user == null){
				return null;
			}
		}*/
		SysUser user = sysUserDao.get(id);
		return user;
	}
	
	/**
	 * 0.获取当前用户的id
	 * @return
	 */
	public static String getUserId() {
		Subject currentUser = SecurityUtils.getSubject();
		if (null != currentUser) {
			Session session = currentUser.getSession();
			SysUser user = (SysUser) session.getAttribute("currentUser");
			return user.getId();
		}
		// 如果没有登录，则返回实例化空的User对象。
		return "";
	}

	/**
	 * 1.获取当前用户
	 * @return
	 */
	public static SysUser getUser() {
		Subject currentUser = SecurityUtils.getSubject();
		if (null != currentUser) {
			Session session = currentUser.getSession();
			SysUser user = (SysUser) session.getAttribute("currentUser");
			return user;
		}
		// 如果没有登录，则返回实例化空的User对象。
		return new SysUser();
	}
	
	/**
	 * 2.获取当前用户的角色的权限集合
	 * 一个用户可能存在多个角色
	 * @return
	 */
	public static List<SysUserRole> getUserRoles(){
		/*SysUser user = getUser();
		if(null != user){
			List<SysUserRole> sysUserRoles = (List<SysUserRole>) RedisUtil.get(USER_ROLE_CACHE+getUserId());
			if(sysUserRoles ==null){ //如果缓存没有再查询
				sysUserRoles = sysUserRoleDao.getUserRoleList(String.valueOf(user.getId())); //根据用户id
				RedisUtil.set(USER_ROLE_CACHE+user.getId(), sysUserRoles);
			}
			return sysUserRoles;
		}
		return null;*/
		SysUser user = getUser();
		if(null != user){
			List<SysUserRole> sysUserRoles = sysUserRoleDao.getUserRoleList(String.valueOf(user.getId())); //根据用户id
			return sysUserRoles;
		}
		return null;
	}
	
	/**
	 * 3.获取当前用户的菜单权限集合
	 * @return
	 */
	public static List<SysRoleMenu> getSysRoleMenus(){
		/*//获取用户角色的权限集合
		List<SysUserRole> sysUserRoles = getUserRoles();
		//获取用户角色菜单的权限集合
		List<SysRoleMenu> sysRoleMenus = (List<SysRoleMenu>) RedisUtil.get(ROLE_MENU_CACHE+getUserId());
		if(sysRoleMenus == null){ //如果缓存没有再查询
			List<String> roleIds = new ArrayList<String>();
			Map<String,Object> map = new HashMap<String,Object>();
			if(null != sysUserRoles){
				for(SysUserRole sysUserRole : sysUserRoles){
					roleIds.add(String.valueOf(sysUserRole.getRole_id()));
				}
				map.put("roleIds", roleIds);
			}
			sysRoleMenus = sysRoleMenuDao.getSysRoleMenus(map);
			RedisUtil.set(ROLE_MENU_CACHE+getUserId(), sysRoleMenus);
		}
		return sysRoleMenus;*/
		//获取用户角色的权限集合
		List<SysUserRole> sysUserRoles = getUserRoles();
		//获取用户角色菜单的权限集合
		List<String> roleIds = new ArrayList<String>();
		Map<String,Object> map = new HashMap<String,Object>();
		if(null != sysUserRoles){
			for(SysUserRole sysUserRole : sysUserRoles){
				roleIds.add(String.valueOf(sysUserRole.getRoleId()));
			}
			map.put("roleIds", roleIds);
		}
		List<SysRoleMenu> sysRoleMenus = sysRoleMenuDao.getSysRoleMenus(map);
		return sysRoleMenus;
	}
	
	/**
	 * 4.根据角色菜单的权限集合中取出菜单的ids作为条件查询当前角色的菜单列表数据
	 * @return
	 */
	public static List<SysMenu> getSysMenus(){
		/*List<SysMenu> sysMenus = (List<SysMenu>) RedisUtil.get(SYS_MENU+getUserId());
		List<SysRoleMenu> getSysRoleMenus = getSysRoleMenus();
		if(null != getSysRoleMenus){
			List<String> menuIds = new ArrayList<String>();
			Map<String,Object> map = new HashMap<String,Object>();
			for(SysRoleMenu sysRoleMenu :getSysRoleMenus){
				menuIds.add(sysRoleMenu.getMenu_id());
			}
			map.put("menuIds", menuIds);
			if(null == sysMenus){ //如果缓存没有再查询
				sysMenus = sysMenuDao.getSysMenus(map);
			}
		}
		return sysMenus;*/
		List<SysMenu> sysMenus = null;
		List<SysRoleMenu> getSysRoleMenus = getSysRoleMenus();
		if(null != getSysRoleMenus){
			List<String> menuIds = new ArrayList<String>();
			Map<String,Object> map = new HashMap<String,Object>();
			for(SysRoleMenu sysRoleMenu :getSysRoleMenus){
				menuIds.add(sysRoleMenu.getMenuId());
			}
			map.put("menuIds", menuIds);
			sysMenus = sysMenuDao.getSysMenus(map);
		}
		return sysMenus;
	}
	
	/**
	 * 5.获取当前用户的角色列表
	 * @return
	 */
	public static List<SysRole> getSysRoles(){
		/*List<SysRole> sysRoles = (List<SysRole>) RedisUtil.get(SYS_ROLE+getUserId());
		if(sysRoles == null){ //如果缓存没有再查询
			Map<String,Object> map = new HashMap<String,Object>();
			List<SysUserRole> getUserRoles = getUserRoles();
			List<String> roleIds = new ArrayList<String>();
			for(SysUserRole sysUserRole :getUserRoles){
				roleIds.add(String.valueOf(sysUserRole.getRole_id()));
			}
			map.put("roleIds", roleIds);
			sysRoles = sysRoleDao.getSysRoles(map);
			RedisUtil.set(SYS_ROLE+getUserId(), sysRoles);
		}
		return sysRoles;*/
		Map<String,Object> map = new HashMap<String,Object>();
		List<SysUserRole> getUserRoles = getUserRoles();
		List<String> roleIds = new ArrayList<String>();
		for(SysUserRole sysUserRole :getUserRoles){
			roleIds.add(String.valueOf(sysUserRole.getRoleId()));
		}
		map.put("roleIds", roleIds);
		List<SysRole> sysRoles = sysRoleDao.getSysRoles(map);
		return sysRoles;		
	}
	
	/**
	 * 获取session信息
	 * @return
	 */
	public static Session getSession(){
		try{
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession(false);
			if (session == null){
				session = subject.getSession();
			}
			if (session != null){
				return session;
			}
//			subject.logout();
		}catch (InvalidSessionException e){
			
		}
		return null;
	}
	
}
